﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SPMS.Common;
using SPMS.Data;
using SPMS.Web.Mvc;
using SPMS.Web.Models;

namespace SPMS.Web.Controllers
{
    /// <summary>
    /// 活动作业控制器。
    /// </summary>
    public class ActiveOperationController : Controller
    {
        #region ---Views

        /// <summary>
        /// 默认视图。
        /// </summary>
        /// <returns>视图结果。</returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 作业登记视图。
        /// </summary>
        /// <returns>视图结果。</returns>
        public ActionResult Register()
        {
            return View();
        }

        /// <summary>
        /// 资料补录试图
        /// </summary>
        /// <returns></returns>
        public ActionResult Manage()
        {
            return View();
        }

        /// <summary>
        /// 资料登记试图
        /// </summary>
        /// <returns></returns>
        public ActionResult Regform()
        {
            return View();
        }
        #endregion

        #region ---Actions

        /// <summary>
        /// 获得操作类型信息列表。
        /// </summary>
        /// <param name="queryModel">查询模型对象。</param>
        /// <returns>数据。</returns>
        [HttpGet]
        [JsonHandleError]
        public ActionResult List(JsonPagingQueryModel queryModel)
        {
            //用于保存总记录数
            using (var context = new AccountFileManagementEntities())
            {
                int totalRecord = 0;

                //查询数据
                var query = from item in context.ActiveOperations.QueryData<ActiveOperation>(queryModel, out totalRecord)
                            select new
                            {
                                item.Operation_ID,
                                item.Operation_Code,
                                item.OperationType_ID,
                                item.Status,
                                item.Account_ID
                                //item.AccountCode,
                                //item.OrganizationName,
                                //item.BusinessAddress,
                                //item.ContactAddress,
                                //item.ContactNumber,
                                //item.Businesslicense_SN,
                                //item.OrganizationCode,
                                //item.TaxRegistrationCertificate_SN,
                                //item.AccountOpeningLicense_SN,
                                //item.LegalPerson_Name,
                                //item.LegalPerson_SN,
                                //item.ContactPerson1,
                                //item.ContactNumber1,
                                //item.ContactEmail1,
                                //item.ContactPerson2,
                                //item.ContactNumber2,
                                //item.ContactEmail2,
                                //item.ContactPerson3,
                                //item.ContactNumber3,
                                //item.ContactEmail3
                            };

                //获得数据
                var data = query.ToArray();
                return this.JsonEx(true, data, totalRecord);
            }
        }


        /// <summary>
        /// 获得操作作业的资料信息列表。
        /// </summary>
        /// <param name="queryModel">查询模型对象。</param>
        /// <returns>数据。</returns>
        [JsonHandleError]
        public ActionResult ListFiles(JsonPagingQueryModel queryModel)
        {
            //用于保存总记录数
            using (var context = new AccountFileManagementEntities())
            {
                int totalRecord = 0;

                //查询数据
                var query = from item in context.ActiveOperationFiles.QueryData<ActiveOperationFile>(queryModel, out totalRecord)
                            select new
                            {
                                item.Operation_ID,
                                item.File_ID,
                                item.DueDate,
                                item.FileType_ID,
                                item.IsDelayedDelivery,
                                item.Memo
                                
                            };

                //获得数据
                var data = query.ToArray();
                return this.JsonEx(true, data, totalRecord);
            }
        }


        /// <summary>
        /// 创建作业。
        /// </summary>
        /// <param name="model">作业模型。</param>
        /// <returns>执行结果。</returns>
        [HttpPost]
        [JsonHandleError]
        ///[PermissionRequirements(new int[] { 10000001 }, new string[] { "SystemAdmin" })]
        public ActionResult Create(ActiveOperations model)
        {

            return null;
        }

        /// <summary>
        /// 获得作业详细信息。
        /// </summary>
        /// <param name="model">作业查询模型。</param>
        /// <returns>
        /// 执行结果。
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        public ActionResult Detail(ActiveOperation model)
        {
            //创建上下文
            using (AccountFileManagementEntities context = new AccountFileManagementEntities())
            {
                //查询数据
                var entity = context.ActiveOperations.FirstOrDefault(item => item.Operation_ID == model.Operation_ID);

                //判断是否有数据
                if (null == entity)
                {
                    //返回结果
                    return this.JsonEx(false, string.Format("编号为'{0}'的作业信息不存在。<br/>请确认此作业是否已经被删除。", model.Operation_Code));
                }
                //创建数据
                var data = new
                {
                    //entity.User_ID,
                    //entity.LoginName,
                    //Organization = entity.Organization.Organization_Code + " " + entity.Organization.Organization_Name,
                    //entity.Organization_ID,
                    //entity.Organization.Organization_Code,
                    //entity.Organization.Organization_Name,
                    //entity.Email,
                    //entity.EmployeeName,
                    //entity.IsAuthorized,
                    //entity.IsBuildin,
                    //entity.IsEnabled,
                    //entity.IsLocked,
                    //entity.MustChangePassword,
                    //entity.LastLoginTime,
                    //entity.PhoneNumber1,
                    //entity.PhoneNumber2,
                    //HasSignature = (null != entity.Signature && entity.Signature.Length > 0),
                    //entity.Title,
                    entity.Operation_ID,
                    entity.OrganizationCode,
                    entity.Status,
                    entity.Operation_Code,
                    entity.OperationType_ID,                    
                    entity.Account_ID

                };
                //返回数据
                return this.JsonEx(true, data, 1);
            }
        }


        /// <summary>
        /// 删除作业。
        /// </summary>
        /// <param name="model">要删除的作业。</param>
        /// <returns>
        /// 执行结果。
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        public ActionResult Delete(ActiveOperation model)
        {
            return null;
        }
        #endregion
    }
}
